package thread;

/**
 *
 */
public class SyncDemo4 {
    public static void main(String[] args) {
        Foo foo = new Foo();
        Thread t1 = new Thread(){
            public void run(){
                foo.mothedA();
            }
        };
        Thread t2 = new Thread(){
            public void run(){
                foo.mothedB();
            }
        };
        t1.start();
        t2.start();

    }
}
class Foo{
    public synchronized void mothedA(){
        try {
            Thread thread = Thread.currentThread();
            System.out.println(thread.getName()+":A正在执行");
            Thread.sleep(5000);
            System.out.println(thread.getName()+":结束A程序");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void mothedB(){
        synchronized(Foo.class){
        {
            try {
                Thread thread = Thread.currentThread();
                System.out.println(thread.getName() + ":B正在执行");
                Thread.sleep(5000);
                System.out.println(thread.getName() + ":结束B程序");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    }
}
